layui.use(['table','layer'], function(){
    var table = layui.table;
    var layer=layui.layer;
    //关联表
    table.render({
        elem: '#contact'
        ,defaultToolbar:[]
        ,toolbar:'#toolbarD'
        ,id:'con'
        ,cols: [[
            {type:'checkbox'}
            ,{field:'id', width:120, title: 'ID'}
            ,{field:'attrname', width:120, title: '属性名'}
            ,{field:'value', width:120, title: '可选值'}
            ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:100}
        ]]
        ,page:true
        ,data:[{
            id:"",
            attrname:"",
            value:""
        }]

    });


    //新增关联表
    table.render({
        elem: '#newcontact'
        ,toolbar:'#toolbarDemo'
        ,defaultToolbar:[]
        ,id:'contactadd'
        ,page:true
        ,cols: [[
            {type:'checkbox'}
            ,{field:'agid', width:100, title: '属性id'}
            ,{field:'attrn', width:100, title: '属性名'}
            ,{field:'value', width:100, title: '属性图标'}
            ,{field:'attr', width:100, title: '可选值列表'}
        ]]
        ,data:[{
            attrname:"",
            attrn:"",
            value:"",
            attr:""
        }]

    });

    table.on('tool(contact)',function(obj){

        var data=obj.data;
        if(obj.event==='delete'){

            var agid=data.agid;
            $.ajax({
                type:"post",
                url:"",
                data:{

                },
                dataType:"json",
                success:function (data) {
                    if (data == 0) {
                        layer.msg('删除失败！',{icon:5,offset:"auto",time:2000});//提示框
                    }else{
                        layer.msg('删除成功！',{icon:6,offset:"auto",time:2000});//提示框
                    }
                    setTimeout(function(){
                        location.reload();//重新加载页面
                    }, 2100);
                }
            })

        }

    });

    table.on('toolbar(contact)',function (obj) {
        var data=obj.data;
        //新增
        if(obj.event==='newcont'){
            layer.open({
                        area:["530px","530px"],
                        type:1,
                        content:$("#newcon"),
                        title:"新增"
                    })
        }
        //批量删除
        else if (obj.event==='pdelete'){
            var checkStatus = table.checkStatus('contact')
                ,checkData = checkStatus.data,
                delList=[];
            for (var i = 0; i < checkData.length; i++) {
                delList.push(checkData[i].id);
            }
            //json对象
            var strify = JSON.stringify(delList);
            console.log(delList);
            console.log(strify);
            $.ajax({
                url:"",
                dataType:'json',
                data:{
                    datas:strify
                },
                type:'post',
                success:function(data){
                    if (data == 0) {
                        layer.msg('删除失败！',{icon:5,offset:"auto",time:2000});//提示框
                    }else{
                        layer.msg('删除成功！',{icon:6,offset:"auto",time:2000});//提示框
                    }

                    setTimeout(function(){
                        location.reload();//重新加载页面表格
                    }, 2100);
                }
            });
            // return false;
        }
    });

    table.on('toolbar(newcontact)',function (obj) {
            var data=obj.data;

            //批量新增
            if(obj.event==='add'){
                var checkStatus = table.checkStatus('contactadd')
                    ,checkData = checkStatus.data,
                    addList=[];
                for (var i = 0; i < checkData.length; i++) {
                    addList.push(checkData[i].agid);
                }
                //json对象
                var strify = JSON.stringify(addList);
                console.log(addList);
                console.log(strify);
                $.ajax({
                    url:"",
                    dataType:'json',
                    data:{
                        datas:strify
                    },
                    type:'post',
                    success:function(data){
                        if (data == 0) {
                            layer.msg('删除失败！',{icon:5,offset:"auto",time:2000});//提示框
                        }else{
                            layer.msg('删除成功！',{icon:6,offset:"auto",time:2000});//提示框
                        }
                        setTimeout(function(){
                            location.reload();//重新加载页面表格
                        }, 2100);
                    }
                });
                // return false;
            }

    });




    //根据属性名查询
    $("#attr").on('click',function () {
        var attrname=$("#attrname").val();


            table.reload('attrn',{
                method:"post",
                url:"",
                where:{
                        attrname:attrname
                }

        })



    })


});                     //结尾